import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta

day = 1 # 1 year

url_gitlab = 'https://invent.kde.org'

url_kwin_pr =  '/plasma/kwin/-/merge_requests?scope=all&sort=merged_at_desc&state=merged'
time_now = datetime.now()

url_next_page = url_kwin_pr
file = open('kwin_pr.md', 'w+')
while (url_next_page):
    r = requests.get(url_gitlab + url_next_page)
    soup = BeautifulSoup(r.text, 'html.parser')
    pr_list = soup.find_all('li',  class_='merge-request merged')
    next_page = soup.find('a', class_='page-link', rel='next')
    if (next_page):
        url_next_page = next_page['href']
    else:
        url_next_page = None
    time_merged = None

    for pr in pr_list:
        pr_title = pr.find('a', class_='js-prefetch-document').text.strip()
        pr_url = url_gitlab +  pr.find('a', class_='js-prefetch-document')['href']
        pr_labels = []

        # get pr label
        label_doc = pr.find('div', class_='gl-mt-1')
        if (label_doc):
            label_list = label_doc.find_all('span', class_='gl-label-text gl-label-text-light')
            for label in label_list:
                pr_labels.append(label.text.strip())

        # get pr time merged
        time_merged_html = pr.find('ul', class_='controls d-flex align-items-center').find('a', class_='has-tooltip')['title']
        time_merged = datetime.strptime(time_merged_html, '%a, %d %b %Y %H:%M:%S %z')
        if (time_merged.date() < (time_now - timedelta(days=day)).date()):
            break

        patch = requests.get(pr_url + '.patch')
        with open(pr_url.split('/')[-1] + '.patch', 'w+') as f:
            f.write(patch.text)
        table_content = f'''\
| Title         | {pr_title}                     |
|---------------|--------------------------------|
| Url           | {pr_url}                       |
| Labels        | {str(pr_labels)}               |
| Time merged   | {time_merged_html}              |
| Patch file    | [{pr_url.split('/')[-1]}.patch]({pr_url.split('/')[-1]}.patch) |
'''
        file.write(table_content + '\n\n')
    if (time_merged.date() < (time_now - timedelta(days=day)).date()):
        file.closed()
        break


